package com.meisiabc.app.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.meisiabc.app.service.FreeAuditionService;
import com.meisiabc.app.service.SmsCodeService;
import com.meisiabc.support.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import static com.meisiabc.app.service.SmsCodeService.FREE_AUDITION;

@RestController
@RequestMapping("/free_audition")
public class FreeAuditionController {
    @Autowired
    private SmsCodeService smsCodeService;
    @Autowired
    private FreeAuditionService freeAuditionService;

    @GetMapping("/sms")
    public Result sms(@RequestParam String mobile) {
        smsCodeService.sendSms(FREE_AUDITION, mobile);
        return Result.ok();
    }

    @PostMapping("/submit")
    public Result sms_submit(@RequestBody JSONObject loginForm) {
        String recommenderMobile = loginForm.getString("recommender_mobile");
        String mobile = loginForm.getString("mobile");
        String smsCode = loginForm.getString("sms_code");
        String enName = loginForm.getString("enName");
        Integer age = loginForm.getInteger("age");

        if (smsCodeService.verifySmsCode(FREE_AUDITION, mobile, smsCode)) {
            freeAuditionService.register(recommenderMobile, mobile, enName, age, LocalDateTime.now());
        }
        return Result.ok();
    }
}
